/*
 *
 * Author Administrator
 * Date 2021/9/6 14:55
 */
package cn.safekeeper.guide.admin.config;

import cn.safekeeper.common.model.SafeKeeperAuthorizationCallBack;
import cn.safekeeper.core.SafeKeeper;
import cn.safekeeper.guide.admin.domain.entity.SysUser;
import cn.safekeeper.guide.admin.service.ISysMenuService;
import cn.safekeeper.guide.admin.service.ISysRoleService;
import cn.safekeeper.guide.admin.service.ISysUserService;
import com.alibaba.fastjson.JSON;
import io.reactivex.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Component
public class UserAuthorization implements SafeKeeperAuthorizationCallBack {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        if (loginId.toString().equals("1")) { //管理员
            return Collections.singletonList("*");
        }
        return menuService.selectPermsByUserId(Long.parseLong(loginId.toString()));
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Set<String> roles = roleService.selectRolePermissionByUserId(Long.parseLong(loginId.toString()));
        return new ArrayList<>(roles);
    }

    @Override
    public String getUserJsonInfo(Object loginId, String loginType) {
        SysUser sysUser = userService.selectUserById(Long.parseLong(loginId.toString()));
        if (sysUser == null) {
            return null;
        }
        return JSON.toJSONString(sysUser);
    }
}
